package com.tool.write.medile;

import com.alibaba.excel.EasyExcel;
import com.tool.write.entity.Person;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author niuyicong
 * @date 2024/10/19  18:26:47
 * @description 动态表头
 */
public class DynamicHeadWrite {
    public static void main(String[] args) {
        String fileName = "动态表头" + System.currentTimeMillis() + ".xlsx";
        // 写法一
        EasyExcel.write(fileName, Person.class)
                .sheet("动态表头")
                .head(head())
                .doWrite(() -> {
                    return data();
                });
    }

    private static List<List<String>> head() {
        List<List<String>> list = new ArrayList<List<String>>();

        List<String> head0 = new ArrayList<String>();   // 子List代表的是列，一个List是一列
        head0.add("姓名" + System.currentTimeMillis());

        List<String> head1 = new ArrayList<String>();
        head1.add("年龄" + System.currentTimeMillis());

        List<String> head2 = new ArrayList<String>();
        head2.add("生日" + System.currentTimeMillis());
        list.add(head0);
        list.add(head1);
        list.add(head2);
        return list;
    }

    public static List<Person> data(){
        List<Person> data = new ArrayList<>();
        Person person1 = new Person("niu",25,new Date(),"男");
        Person person2 = new Person("niuniu",26,new Date(),"男");
        Person person3 = new Person("niuniuniu",27,new Date(),"男");
        data.add(person1);
        data.add(person2);
        data.add(person3);
        return data;
    }
}
